﻿using UnityEngine;
using System.Collections;

public class PlayerPlan :Plane
{
	// Use this for initialization
	private Rigidbody2D rig;

	public GameObject bulletPrefab;
	public AudioClip ShootSond;
	private AudioSource audioSource;

	 protected override void Start () {
		base.Start ();
		rig = GetComponent<Rigidbody2D> ();
		InvokeRepeating ("Shoot",0,0.05f);
		audioSource = gameObject.AddComponent <AudioSource > ();
	}
	
	// Update is called once per frame
	void Update ()
	{
	
		float h = Input.GetAxis ("Horizontal");
		float v = Input.GetAxis ("Vertical");
		Vector2 dir = new Vector2 (h, v).normalized;
		rig.velocity = dir;

		Vector2 pos = transform.position;
		pos.y = Mathf.Clamp (pos.y, -4.3f, 4.5f);


		if (h < 0 && pos.x < -3) {
			pos.x = -3;
		} else if (h > 0 && pos.x > 3) {
			pos.x = -3;
		}
		transform.position = pos;
	}
		void Shoot()
		{
		Instantiate (bulletPrefab ,transform.position ,transform .rotation);
		audioSource.PlayOneShot (ShootSond);
		}
	}
	
